using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Callbacks;
using UnityEngine;

public class AssetManager : MonoBehaviour
{
    public static AssetManager instance;



    void Awake()
    {
        instance ??= this;
    }

    // Update is called once per frame
    public GameObject LoadGameObject(string assetBundlePath, string assetName)
    {
        AssetBundle bundle = AssetBundle.LoadFromFile(assetBundlePath);
        UnityEngine.Object obj = bundle.LoadAsset(assetName);
        return (GameObject)Instantiate(obj);
    }

    // void AfterAssetBundleLoaded(AssetBundle bundle)
    //{

    //  }

    public void LoadGameObjectAsyc(string assetBundlePath, string assetName, Action<UnityEngine.Object> action)
    {
        // StartCoroutine(StartLoadAssetBundle(assetBundlePath, AfterAssetBundleLoaded));

        StartCoroutine(StartLoadAssetBundle(assetBundlePath, (bundle) =>
        {
            UnityEngine.Object obj = bundle.LoadAsset(assetName);
            action.Invoke(obj);
        }));
        IEnumerator StartLoadAssetBundle(string assetBundlePath, Action<AssetBundle> action)
        {
            AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(assetBundlePath);
            yield return request;
            action?.Invoke(request.assetBundle);
        }
    }
}
